package com.example.lotterysystem.controller;

import com.example.lotterysystem.common.Result;
import com.example.lotterysystem.common.enums.GlobalErrorCodeEnum;
import com.example.lotterysystem.common.exception.GlobalException;
import com.example.lotterysystem.common.utils.JacksonUtil;
import com.example.lotterysystem.controller.param.BatchDeleteParams;
import com.example.lotterysystem.controller.param.CreatePrizeParam;
import com.example.lotterysystem.controller.param.PageParam;
import com.example.lotterysystem.controller.result.PrizeInfoResult;
import com.example.lotterysystem.service.PictureService;
import com.example.lotterysystem.service.PrizeService;
import com.example.lotterysystem.service.dto.PageListDTO;
import com.example.lotterysystem.service.dto.PrizeDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class PrizeController {

    @Autowired
    private PictureService pictureService;
    @Autowired
    private PrizeService prizeService;

    @RequestMapping("/pic/upload")
    public String uploadPic(MultipartFile file){
        return pictureService.savePicture(file);
    }

    /**
     * 创建奖品，通过表单提交数据
     * @param param
     * @param file
     * @return
     */
    @RequestMapping("/prize/create")
    public Result<Long> createPrize(@Validated
               @RequestPart("param") CreatePrizeParam param,
               @RequestPart("prizePic") MultipartFile file){
        log.info("PrizeController createPrize: {}", JacksonUtil.writeValueAsString(param));
        return Result.success(prizeService.createPrize(param, file));
    }

    @RequestMapping("/prize/getPrizeInfoList")
    public Result<PrizeInfoResult> getPrizeList(PageParam param){
        log.info("PrizeController getPrizeList: {}", JacksonUtil.writeValueAsString(param));
        PageListDTO<PrizeDTO> prizeList = prizeService.getPrizeList(param);
        return Result.success(transfer(prizeList));
    }

    @RequestMapping("/prize/deletePrize")
    public Result<Boolean> deletePrizeById(Long prizeId){
        log.info("PrizeController deletePrizeById: {}", prizeId);
        boolean result = prizeService.deletePrizeById(prizeId);
        return Result.success(result);
    }

    @RequestMapping("/prize/batchDeletePrizes")
    public Result<Boolean> batchDeletePrizesByIds(@RequestBody BatchDeleteParams idList){
        log.info("PrizeController batchDeletePrizesByIds: {}",
                JacksonUtil.writeValueAsString(idList));
        boolean result = prizeService.batchDeletePrizesByIds(idList.getPrizeIds());
        return Result.success(result);
    }
    private PrizeInfoResult transfer(PageListDTO<PrizeDTO> prizeList) {
        if(prizeList == null){
            throw new GlobalException(GlobalErrorCodeEnum.PRIZE_LIST_ERROR);
        }
        PrizeInfoResult prizeInfoResult = new PrizeInfoResult();
        prizeInfoResult.setTotal(prizeList.getTotal());
        // 将List<PrizeDTO> 转化为 List<PrizeInfo>
        List<PrizeInfoResult.PrizeInfo> prizeInfos = new ArrayList<>();
        List<PrizeDTO> records = prizeList.getRecords();
        for(PrizeDTO prizeDTO : records){
            PrizeInfoResult.PrizeInfo prizeInfo = new PrizeInfoResult.PrizeInfo();
            prizeInfo.setPrizeId(prizeDTO.getPrizeId());
            prizeInfo.setPrizeName(prizeDTO.getPrizeName());
            prizeInfo.setDescription(prizeDTO.getDescription());
            prizeInfo.setPrice(prizeDTO.getPrice());
            prizeInfo.setImageUrl(prizeDTO.getImageUrl());
            prizeInfos.add(prizeInfo);
        }
        prizeInfoResult.setRecords(prizeInfos);
//        prizeInfoResult.setRecords(prizeList.getRecords().stream().map(prizeDTO -> {
//            PrizeInfoResult.PrizeInfo prizeInfo = new PrizeInfoResult.PrizeInfo();
//            prizeInfo.setPrizeId(prizeDTO.getPrizeId());
//            prizeInfo.setPrizeName(prizeDTO.getPrizeName());
//            prizeInfo.setDescription(prizeDTO.getDescription());
//            prizeInfo.setPrice(prizeDTO.getPrice());
//            prizeInfo.setImageUrl(prizeDTO.getImageUrl());
//            return prizeInfo;
//        }).toList());
        return prizeInfoResult;
    }
}
